﻿using System;
using System.Collections.Generic;
using System.Text;
using PPI.Entities.Lookup;
using PPI.Entities.Inspections;
using PPI.General;

namespace PPI.Entities.Distribution
{
    public enum DistributionType
    {
        GPS,
        Inspection,
        None
    }

    public class EDistributionPole : EBasePosition
    {
        #region Properties
        public Int32 Id {get;set;}
        public EMap Map {get;set;}
        public ESection Section { get; set; }
        public List<EDistributionPoleImage> Images { get; set; }
        public List<ENote> Notes { get; set; }
        public List<EPhase> Phases { get; set; }
        public List<EXMissionStructures_PoleTopType> Tops { get; set; }
        public EXMissionStructures_PoleSpecies Specie { get; set; }
        public EXMissionStructures_PoleTreatment Treatment { get; set; }
        public EXMissionStructures_PoleClass Class { get; set; }
        public EXMissionStructures_PoleLength Length { get; set; }
        public EDistributionInspectionResult InspectionResult { get; set; }
        public EDistributionLookUp Kv { get; set; }
        public EDistributionLookUp ConductorType { get; set; }
        public EDistributionLookUp ACSR { get; set; }
        public string InspBy { get; set; }
        public string Number { get; set; }
        public string D804 { get; set; }
        public string Circuit { get; set; }
        public DateTime AcquisitionDate { get; set; }
        public DateTime LVisit { get; set; }
        public Int32 Year { get; set; }
        public DistributionType DistributionType { get; set; }
        public List<EMeter> Meters { get; set; }
        public List<ETransformer> Transformers { get; set; }
        public List<ESectionalizer> Sectionalizers { get; set; }
        public List<ERecloser> Reclosers { get; set; }
        public int GuardsQuantity { get; set; }
        public bool GroundWireRepair { get; set; }
        public bool PoleTag { get; set; }
        public bool Billable { get; set; }
        public bool isProjectPole {get;set;}

        public EVendor Vendor { get; set; }
        public EVendor_Contact VendorContact { get; set; }
        public EInspector Inspector { get; set; }
        public EVendor_Project Project { get; set; }

         
        #endregion

        public EDistributionPole()
        {
            Map = new EMap();
            Section = new ESection();
            Kv = new EDistributionLookUp();
            ConductorType = new EDistributionLookUp();
            ACSR = new EDistributionLookUp();
            Specie = new EXMissionStructures_PoleSpecies();
            Class = new EXMissionStructures_PoleClass();
            Length = new EXMissionStructures_PoleLength();
            Treatment = new EXMissionStructures_PoleTreatment();
            Images = new List<EDistributionPoleImage>();
            Meters = new List<EMeter>();
            Transformers = new List<ETransformer>();
            Sectionalizers = new List<ESectionalizer>();
            Reclosers = new List<ERecloser>();
            InspectionResult = new EDistributionInspectionResult();
            InspBy = string.Empty;
            Number = string.Empty;
            D804 = string.Empty;
            Circuit = string.Empty;
            Year = Int32.MinValue;
            LVisit = DateTime.MinValue;
            AcquisitionDate = DateTime.MinValue;
            Tops = new List<EXMissionStructures_PoleTopType>();
            Phases = new List<EPhase>();
            Images = new List<EDistributionPoleImage>();
            Notes = new List<ENote>();
            isProjectPole = false;
            Vendor = new EVendor();
            Inspector = new EInspector();
            Project = new EVendor_Project();
        }

        public override string GetHtmlPlacemark()
        {
            StringBuilder _Shtml = new StringBuilder();
            string distributionDescription = DistributionType == DistributionType.Inspection ? "Inspection" : "GPS";
            string poleDescription = GetHtmlName();
            int colspan = 1;

            if (Meters.Count > 0)
                colspan = 2;

            _Shtml.AppendLine("<table style='font-family:Verdana; font-size:10px;' border='0'>");
            _Shtml.AppendLine("     <tr>");
            _Shtml.AppendLine("         <td colspan='" + colspan + "'>");
            _Shtml.AppendLine("             <b>Distribution " + distributionDescription.Trim() + " - " + poleDescription + " </b>");
            _Shtml.AppendLine("         </td>");
            _Shtml.AppendLine("     </tr>");

            _Shtml.AppendLine("     <tr>");
            _Shtml.AppendLine("         <td>");
            _Shtml.AppendLine(              GetHtmlPoleHeader());
            _Shtml.AppendLine("         </td>");
            
            if (Meters.Count > 0)
            {
                _Shtml.AppendLine("         <td>");
                _Shtml.AppendLine(GetHtmlMetersTable());
                _Shtml.AppendLine("         </td>");
            }

            _Shtml.AppendLine("     </tr>");


            _Shtml.AppendLine("</table>");


            /*
            _Shtml.AppendLine("<table style='font-family:Verdana; font-size:10px;' border='0'>");

            _Shtml.AppendLine("<tr>");
            if (Images.Count > 0 || Meters.Count > 0) 
                _Shtml.AppendLine("     <td colspan='2'>");
            else
                _Shtml.AppendLine("     <td>");
            _Shtml.AppendLine(          GetHtmlPoleHeader());
            _Shtml.AppendLine("     </td>");
            _Shtml.AppendLine("</tr>");

            if (Images.Count > 0 || Meters.Count > 0) 
            {
                _Shtml.AppendLine("<tr>");
                
                if (Images.Count > 0) 
                {
                    _Shtml.AppendLine("     <td>");
                    _Shtml.AppendLine(          GetHtmlPoleImagesTable());
                    _Shtml.AppendLine("     </td>");
                }

                if (Meters.Count > 0)
                {
                    _Shtml.AppendLine("     <td>");
                    _Shtml.AppendLine(          GetHtmlMetersTable());
                    _Shtml.AppendLine("     </td>");
                }
                
                _Shtml.AppendLine("</tr>");
            }

            _Shtml.AppendLine("</table>");
             */
            
            return _Shtml.ToString();
        }

        public string GetHtmlPoleImagesTable()
        {
            StringBuilder htmlImages = new StringBuilder();

            if (isProjectPole) {
                ConfigReader reader = ConfigReader.GetInstance();
                string mode = reader.ConectionMode;
                string type = GetPoleImageType();

                htmlImages.AppendLine("<table>");

                foreach (EDistributionPoleImage image in Images)
                {
                    string directoryName = DirHelper.GetImageDirectory(Id, image.Id);
                    string fileName = Id + "_" + image.Id;
                    htmlImages.AppendLine("<tr>");
                    htmlImages.AppendLine("     <td>");                
                    htmlImages.AppendLine(
                        "<a href=\"#DistributionPoleImage_" + image.Id +";balloon\"><img src=\"http://www.dev-poleposition.com/Deploy/WebSite2.1/ImagePreview.aspx?Type=" + type + "&mode=" +
                        mode + "&dn=" + directoryName + "&fn=" + fileName + "&w=" + Constants.k_PREVIEWIMAGEWIDTH + "&h=" + Constants.k_PREVIEWIMAGEHEIGHT + "\"></a>");

                    htmlImages.AppendLine("     </td>");
                    htmlImages.AppendLine("</tr>");

                    image.ParentId = Id;
                    image.isProject = isProjectPole;
                    image.ImageServerType = type;
                    image.ParentPlaceMark = GetPlacemarkId();
                }

                htmlImages.AppendLine("</table>");
            }
            else
            {
                htmlImages.AppendLine("<table>");
                //htmlImage = "     <td><a href=\"#" + GetPlacemarkId() + ";balloon\"><img src=\"" + ImageFile.FullName + "\"></a></td>";
                foreach (EDistributionPoleImage image in Images)
                {
                    htmlImages.AppendLine("<tr>");
                    htmlImages.AppendLine("     <td>");
                    htmlImages.AppendLine("         <a href=\"#DistributionPoleImage_" + image.Id + ";balloon\"><img src=\"" + image.FullName + "\"></a>");
                    htmlImages.AppendLine("     </td>");
                    htmlImages.AppendLine("</tr>");

                    image.ParentId = Id;
                    image.isProject = isProjectPole;
                    image.ParentPlaceMark = GetPlacemarkId();
                }

                htmlImages.AppendLine("</table>");                                
            }

            return htmlImages.ToString();
        }

        public string GetHtmlMetersTable()
        {
            StringBuilder htmlHeader = new StringBuilder();

            htmlHeader.AppendLine("<table>");

            htmlHeader.AppendLine("     <tr>");
            foreach (EMeter meter in Meters)
                htmlHeader.AppendLine("     <td><b>Meter [" + meter.Id + "]</b></td>");
            htmlHeader.AppendLine("</tr>");

            htmlHeader.AppendLine("<tr>");
            foreach (EMeter meter in Meters)
                htmlHeader.AppendLine("     <td><b>Number:</b> " + meter.Number.Trim() + "</td>");                
            htmlHeader.AppendLine("</tr>");

            htmlHeader.AppendLine("<tr>");
            foreach (EMeter meter in Meters)
            {
                htmlHeader.AppendLine("     <td>");
                htmlHeader.AppendLine("             <table style='font-family:Verdana; font-size:10px;' cellpadding='0' cellspacing='0' border='0'>");
                htmlHeader.AppendLine("                 <tr>");
                htmlHeader.AppendLine("                     <td valign='top'><b>Notes:</b> </td>");
                htmlHeader.AppendLine("                 </tr>");
                foreach (ENote note in meter.Notes)
                {
                    htmlHeader.AppendLine("             <tr>");
                    htmlHeader.AppendLine("                 <td>- ");
                    htmlHeader.AppendLine(                      note.Name.Trim());
                    htmlHeader.AppendLine("                 </td>");
                    htmlHeader.AppendLine("             </tr>");
                }
                htmlHeader.AppendLine("             </table>");
                htmlHeader.AppendLine("     </td>");
            }
            htmlHeader.AppendLine("</tr>");

            htmlHeader.AppendLine("<tr>");
            foreach (EMeter meter in Meters)
                htmlHeader.AppendLine("     <td><b>Comments:</b> " + meter.Comments.Trim() + "</td>");
            htmlHeader.AppendLine("</tr>");

            htmlHeader.AppendLine("<tr>");
            foreach (EMeter meter in Meters)
                htmlHeader.AppendLine("     <td>" + GetHtmlMeterImage(meter) + "</td>");
            htmlHeader.AppendLine("</tr>");

            htmlHeader.AppendLine("</table>");

            return htmlHeader.ToString();
        }

        public string GetHtmlPoleHeader()
        {
            StringBuilder htmlHeader = new StringBuilder();

            htmlHeader.AppendLine("<table style='font-family:Verdana; font-size:10px;' border='0'>");
            htmlHeader.AppendLine("     <tr>");
            htmlHeader.AppendLine("         <td><b>Project:</b> " + Project.Name + "</td>");
            htmlHeader.AppendLine("     </tr>");
            htmlHeader.AppendLine("     <tr>");
            htmlHeader.AppendLine("         <td><b>Contractor:</b> " + Vendor.Name + "</td>");
            htmlHeader.AppendLine("     </tr>");

            htmlHeader.AppendLine("     <tr>");
            htmlHeader.AppendLine("         <td width='150px'><b>Map:</b> " + Map.Name.Trim() + "</td>");
            htmlHeader.AppendLine("     </tr>");
            htmlHeader.AppendLine("     <tr>");
            htmlHeader.AppendLine("         <td width='150px'><b>Section:</b> " + Section.Name.Trim() + "</td>");
            htmlHeader.AppendLine("     </tr>");
            htmlHeader.AppendLine("     <tr>");
            htmlHeader.AppendLine("         <td width='150px'><b>Species:</b> " + Specie.Name.Trim() + "</td>");
            htmlHeader.AppendLine("     </tr>");
            htmlHeader.AppendLine("     <tr>");
            htmlHeader.AppendLine("         <td width='150px'><b>Class:</b> " + Class.Name.Trim() + "</td>");
            htmlHeader.AppendLine("     </tr>");
            htmlHeader.AppendLine("     <tr>");
            htmlHeader.AppendLine("         <td width='150px'><b>Length:</b> " + Length.Name.Trim() + "</td>");
            htmlHeader.AppendLine("     </tr>");
            htmlHeader.AppendLine("     <tr>");
            htmlHeader.AppendLine("         <td width='150px'><b>Treatment:</b> " + Treatment.Name.Trim() + "</td>");
            htmlHeader.AppendLine("     </tr>");

            htmlHeader.AppendLine("     <tr>");
            htmlHeader.AppendLine("         <td><b>Last Insp:</b> " + InspBy.Trim() + "</td>");
            htmlHeader.AppendLine("     </tr>");
            htmlHeader.AppendLine("     <tr>");
            htmlHeader.AppendLine("         <td><b>Number:</b> " + Number.Trim() + "</td>");
            htmlHeader.AppendLine("     </tr>");
            htmlHeader.AppendLine("     <tr>");
            htmlHeader.AppendLine("         <td><b>Last Insp.:</b> " + LVisit.Date.Year.ToString().Trim() + "</td>");
            htmlHeader.AppendLine("     </tr>");
            htmlHeader.AppendLine("     <tr>");
            htmlHeader.AppendLine("         <td><b>Acquisition Date:</b> " + AcquisitionDate.ToLongDateString().Trim() + "</td>");
            htmlHeader.AppendLine("     </tr>");
            htmlHeader.AppendLine("     <tr>");
            htmlHeader.AppendLine("         <td><b>Year:</b> " + Year + "</td>");
            htmlHeader.AppendLine("     </tr>");


            if (DistributionType == DistributionType.Inspection)
            {
                htmlHeader.AppendLine("     <tr>");
                htmlHeader.AppendLine("         <td><b>Electrical:</b> " + InspectionResult.IsElectrical + "</td>");
                htmlHeader.AppendLine("     </tr>");
                htmlHeader.AppendLine("     <tr>");
                htmlHeader.AppendLine("         <td><b>Result:</b> " + InspectionResult.Resultype.Name.Trim() + "</td>");
                htmlHeader.AppendLine("     </tr>");
                htmlHeader.AppendLine("     <tr>");
                htmlHeader.AppendLine("         <td><b>Inspection:</b> " + InspectionResult.InspectionType.Name.Trim() + "</td>");
                htmlHeader.AppendLine("     </tr>");
                htmlHeader.AppendLine("     <tr>");
                htmlHeader.AppendLine("         <td><b>Inspector:</b> " + Inspector.Name + "</td>");
                htmlHeader.AppendLine("     </tr>");

            }

            htmlHeader.AppendLine("     <tr>");
            htmlHeader.AppendLine("         <td><b>Meters:</b> " + Meters.Count + "</td>");
            htmlHeader.AppendLine("     </tr>");
            htmlHeader.AppendLine("     <tr>");
            htmlHeader.AppendLine("         <td>");
            htmlHeader.AppendLine("             <table style='font-family:Verdana; font-size:10px;' cellpadding='0' cellspacing='0' border='0'>");
            htmlHeader.AppendLine("                 <tr>");
            htmlHeader.AppendLine("                     <td><b>Top:</b> </td>");
            htmlHeader.AppendLine("                 </tr>");
            foreach (EXMissionStructures_PoleTopType top in Tops)
            {
                htmlHeader.AppendLine("             <tr>");
                htmlHeader.AppendLine("                 <td>");
                htmlHeader.AppendLine("- " + top.Name.Trim());
                htmlHeader.AppendLine("                 </td>");
                htmlHeader.AppendLine("             </tr>");
            }
            htmlHeader.AppendLine("             </table>");
            htmlHeader.AppendLine("         </td>");
            htmlHeader.AppendLine("     </tr>");
            htmlHeader.AppendLine("     <tr>");
            htmlHeader.AppendLine("         <td>");
            htmlHeader.AppendLine("             <table style='font-family:Verdana; font-size:10px;' cellpadding='0' cellspacing='0' border='0'>");
            htmlHeader.AppendLine("                 <tr>");
            htmlHeader.AppendLine("                     <td valign='top'><b>Phase:</b></td>");
            htmlHeader.AppendLine("                 </tr>");
            foreach (EPhase phase in Phases)
            {
                htmlHeader.AppendLine("             <tr>");
                htmlHeader.AppendLine("                 <td>");
                htmlHeader.AppendLine("- " + phase.Name.Trim());
                htmlHeader.AppendLine("                 </td>");
                htmlHeader.AppendLine("             </tr>");
            }
            htmlHeader.AppendLine("             </table>");
            htmlHeader.AppendLine("         </td>");
            htmlHeader.AppendLine("     </tr>");

            
            if (DistributionType == Distribution.DistributionType.Inspection)
            {
                htmlHeader.AppendLine("     <tr>");
                htmlHeader.AppendLine("         <td align='left' valign='top'>");
                htmlHeader.AppendLine("             <table style='font-family:Verdana; font-size:10px;' cellpadding='0' cellspacing='0' border='0'>");
                htmlHeader.AppendLine("                 <tr>");
                htmlHeader.AppendLine("                     <td valign='top'><b>Notes:</b> </td>");
                htmlHeader.AppendLine("                 </tr>");
                foreach (ENote note in Notes)
                {
                    htmlHeader.AppendLine("             <tr>");
                    htmlHeader.AppendLine("                 <td>- ");
                    htmlHeader.AppendLine(note.Name.Trim());
                    htmlHeader.AppendLine("                 </td>");
                    htmlHeader.AppendLine("             </tr>");
                }
                htmlHeader.AppendLine("             </table>");
                htmlHeader.AppendLine("         </td>");
                htmlHeader.AppendLine("     </tr>");

                htmlHeader.AppendLine("     <tr>");
                htmlHeader.AppendLine("         <td><b>Comments:</b> " + InspectionResult.Comments.Trim() + "</td>");
                htmlHeader.AppendLine("     </tr>");
            }            

            if (Images.Count > 0)
            {
                htmlHeader.AppendLine("     <tr>");
                htmlHeader.AppendLine("         <td>" + GetHtmlPoleImagesTable() + "</td>");
                htmlHeader.AppendLine("     </tr>");
            }

            htmlHeader.AppendLine("</table>");

            return htmlHeader.ToString();
        }

        public string GetHtmlMeterImage(EMeter meter)
        {
            StringBuilder htmlImages = new StringBuilder();

            if (isProjectPole) 
            {
                ConfigReader reader = ConfigReader.GetInstance();
                string mode = reader.ConectionMode;
                string type = GetPoleImageMeterType();

                string directoryName = DirHelper.GetImageDirectory(Id, meter.Id);
                string fileName = Id + "_" + meter.Id;

                htmlImages.AppendLine("<a href=\"#DistributionMeterPoleImage_" + meter.Id + ";balloon\"><img src=\"http://www.dev-poleposition.com/Deploy/WebSite2.1/ImagePreview.aspx?Type=" + type + "&mode=" +
                    mode + "&dn=" + directoryName + "&fn=" + fileName + "&w=" + Constants.k_PREVIEWIMAGEWIDTH + "&h=" + Constants.k_PREVIEWIMAGEHEIGHT + "\"></a>");

                meter.Image.ImageServerType = type;
            }
            else
            {
                htmlImages.AppendLine("         <a href=\"#DistributionMeterPoleImage_" + meter.Image.Id + ";balloon\"><img src=\"" + meter.Image.FullName + "\"></a>");
            }

            meter.Image.ParentId = Id;
            meter.Image.Id = meter.Id;
            meter.Image.isProject = isProjectPole;
            //meter.Image.ParentPlaceMark = GetPlacemarkId();

            return htmlImages.ToString();            
        }

        public override string GetIcon()
        {
            if (DistributionType == Distribution.DistributionType.Inspection) 
            {
                string returnIcon = string.Empty;
                Int32 value = EResultType.ParseValue(InspectionResult.Resultype.Name);

                if (InspectionResult.IsElectrical && value == 1)
                {
                    returnIcon = Constants.K_INSPECTIONELECPROBICON;
                }
                else
                {
                    switch (value)
                    {
                        case 1:
                            returnIcon = Constants.K_INSPECTIONRESULT1;
                            break;
                        case 2:
                            returnIcon = Constants.K_INSPECTIONRESULT2;
                            break;
                        case 30:
                        case 31:
                        case 32:
                        case 33:
                        case 34:
                            returnIcon = Constants.K_INSPECTIONRESULT3;
                            break;
                    }
                }

                return returnIcon;
            }

            return base.GetIcon();
        }


        private string GetPoleImageType()
        {
            string imageType;

            if (DistributionType == Distribution.DistributionType.GPS)
                imageType = "3";
            else
                imageType = "5";

            return imageType;
        }

        private string GetPoleImageMeterType()
        {
            string imageType;

            if (DistributionType == Distribution.DistributionType.GPS)
                imageType = "4";
            else
                imageType = "6";

            return imageType;
        }

        public override string GetPlacemarkId()
        {
            return "Pole_" + Id;
        }

        public override string GetHtmlName()
        {
            //return "Pole[" + Id + "]";
            return "[" + Id + "] " + (D804.Equals(string.Empty) ? string.Empty : D804.Trim()) ;
        }

        public override bool Equals(object obj)
        {
            if (obj is EDistributionPole)
            {
                EDistributionPole tmpObj = (EDistributionPole)obj;

                return (Id == tmpObj.Id);
            }
            
            return base.Equals(obj);
        }
    }
}
